package com.mx.tool.helper.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.mx.tool.helper.IEncryp;
import com.mx.tool.util.NotifyUtil;
import com.mx.tool.util.NewStrUtil;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author: 徐建鹏.
 * @create: 2021-11-16 14:43
 * @Description:
 */
public class RC4Encryp implements IEncryp {

    private String keys;

    @Override
    public String encrypt(String content, byte[] key) {
        return null;
    }

    @Override
    public String decrypt(String content, byte[] key) {
        return null;
    }

    @Override
    public String encrypt(String content, String key) {
        if (StringUtils.isBlank(key)) {
            key = IdUtil.fastSimpleUUID();
        }
        keys = key;
        try {
            return NewStrUtil.newStringUtf8(Base64.encode(SecureUtil.rc4(key).encrypt(content), false));
        } catch (Exception e) {
            NotifyUtil.error(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String decrypt(String content, String key) {
        if (StringUtils.isBlank(key)) {
            NotifyUtil.error("解密密钥要不能为空");
            throw new RuntimeException("解密密钥要不能为空");
        }
        try {
            return SecureUtil.rc4(key).decrypt(Base64.decode(content));
        } catch (Exception e) {
            NotifyUtil.error(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public byte[] getKeys() {
        return new byte[0];
    }

    @Override
    public String getKey() {
        return keys;
    }
}
